package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.system.domain.HmzProducts;
import com.ruoyi.system.mapper.HmzProductMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HmzProduct;
import com.ruoyi.system.service.IHmzProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用于存储产品信息的Controller
 *
 * @author ruoyi
 * @date 2023-11-10
 */
@RestController
@RequestMapping("/system/product")
public class HmzProductController extends BaseController {
    @Autowired
    private IHmzProductService hmzProductService;
    @Autowired
    private HmzProductMapper hmzProductMapper;


    @GetMapping("/getStatus")
    public AjaxResult getStatus() {
        return success(200);
    }

    /**
     * 导入
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {

        ExcelUtil<HmzProduct> util = new ExcelUtil<>(HmzProduct.class);
        List<HmzProduct> stuList = util.importExcel(file.getInputStream());
        String productName = stuList.get(0).getProductName();
        if (StringUtil.isEmpty(productName)) {
            return AjaxResult.success("请输入产品名称");
        }
        String operName = getUsername();
        String message = hmzProductService.importUser(stuList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 大数据看板展示
     */
    @RequestMapping("/getNumber")
    public AjaxResult getNumber() {
        Date time = new Date();
//        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
//        String format = simpleDate.format(time);
        //在库 已领用 今日入库 今日领用
        JSONObject JSONObject = new JSONObject();
        HmzProduct hmzProduct = new HmzProduct();
        hmzProduct.setProductStatus("在库");
        List<HmzProduct> zaikulist = hmzProductMapper.selectHmzProductList(hmzProduct);
        JSONObject.put("zaikulist", zaikulist.size());
        HmzProduct hmzProduct2 = new HmzProduct();
        hmzProduct2.setProductStatus("已出库");
        List<HmzProduct> yllist = hmzProductMapper.selectHmzProductList(hmzProduct2);
        JSONObject.put("yllist", yllist.size());
        HmzProduct hmzProduct3 = new HmzProduct();
        hmzProduct3.setProductStatus("在库");
        hmzProduct3.setUpdateTime(time);
        List<HmzProduct> zaikulistjr = hmzProductMapper.selectHmzProductList(hmzProduct3);
        JSONObject.put("zaikulistjr", zaikulistjr.size());
        HmzProduct hmzProduct4 = new HmzProduct();
        hmzProduct4.setProductStatus("已出库");
        hmzProduct4.setUpdateTime(time);
        List<HmzProduct> yllistjr = hmzProductMapper.selectHmzProductList(hmzProduct4);
        JSONObject.put("yllistjr", yllistjr.size());
        return AjaxResult.success(JSONObject);
    }


    /**
     * 导入空模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<HmzProducts> util = new ExcelUtil<>(HmzProducts.class);
        util.importTemplateExcel(response, "库存信息");
    }

    /**
     * 查询用于存储产品信息的列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(HmzProduct hmzProduct) {
        startPage();
        List<HmzProduct> list = hmzProductService.selectHmzProductList(hmzProduct);
        return getDataTable(list);
    }

    //    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @RequestMapping("/getInfo")
    public TableDataInfo getInfo(String rfidNumber) {
        HmzProduct hmzProduct = new HmzProduct();
        hmzProduct.setRfidNumber(rfidNumber);
        List<HmzProduct> list = hmzProductService.selectHmzProductList(hmzProduct);
        return getDataTable(list);
    }

    /**
     * 查询产品状态是 已发卡 未入库 /system/product/listRk
     *
     * @param hmzProduct
     * @return
     */
    @GetMapping("/listRk")
    public TableDataInfo listRk(HmzProduct hmzProduct) {
        hmzProduct.setFkStatus("已发卡");
        hmzProduct.setProductStatus("未入库");
        startPage();
        List<HmzProduct> list = hmzProductService.selectHmzProductList(hmzProduct);
        return getDataTable(list);
    }

    /**
     * 盘点查询专用  已发卡 在库 /system/product/listPD
     *
     * @param
     * @return
     */
    @GetMapping("/listPD")
    public TableDataInfo listPD(HmzProduct hmzProduct) {
        hmzProduct.setFkStatus("已发卡");
        hmzProduct.setProductStatus("在库");
        startPage();
        List<HmzProduct> list = hmzProductService.selectHmzProductList(hmzProduct);
        return getDataTable(list);
    }

    /**
     * 导出用于存储产品信息的列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:export')")
    @Log(title = "用于存储产品信息的", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HmzProduct hmzProduct) {
        List<HmzProduct> list = hmzProductService.selectHmzProductList(hmzProduct);
        ExcelUtil<HmzProduct> util = new ExcelUtil<HmzProduct>(HmzProduct.class);
        util.exportExcel(response, list, "用于存储产品信息的数据");
    }

    /**
     * 获取用于存储产品信息的详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hmzProductService.selectHmzProductById(id));
    }

    /**
     * 新增用于存储产品信息的
     */
    @PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "用于存储产品信息的", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HmzProduct hmzProduct) {
        return toAjax(hmzProductService.insertHmzProduct(hmzProduct));
    }

    /**
     * 修改用于存储产品信息的
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "用于存储产品信息的", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HmzProduct hmzProduct) {
        return toAjax(hmzProductService.updateHmzProduct(hmzProduct));
    }

    /**
     * 删除用于存储产品信息的
     */
    @PreAuthorize("@ss.hasPermi('system:product:remove')")
    @Log(title = "用于存储产品信息的", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hmzProductService.deleteHmzProductByIds(ids));
    }
}
